﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;

namespace Onshi.POS.Modelo.Objectos
{
    public class Validaciones
    {
        #region Metodos

        public static Boolean ValidarEmail(String email)
        {
            try
            {
                new MailAddress(email);
                return true;
            }
            catch (FormatException)
            {
                return false;
            }
        }

        public static Boolean ValidarCuit(String cuit)
        {
            if (cuit == null)
                return false;
            cuit = cuit.Replace("-", String.Empty);
            if (!EsNumero(cuit))
                return false;
            if (cuit.Length != 11)
                return false;
            Int32 calculado = CalcularDigitoCuit(cuit);
            Int32 digito = Convert.ToInt32(cuit.Substring(10));
            return calculado == digito;
        }

        private static Int32 CalcularDigitoCuit(String cuit)
        {
            Int32[] multiplicadores = new Int32[] { 5, 4, 3, 2, 7, 6, 5, 4, 3, 2 };
            Char[] nums = cuit.ToCharArray();
            Int32 total = 0;
            for (Int32 i = 0; i < multiplicadores.Length; i++)
                total += Convert.ToInt32(nums[i].ToString()) * multiplicadores[i];
            var resto = total % 11;
            return resto == 0 ? 0 : resto == 1 ? 9 : 11 - resto;
        }

        private static Boolean EsNumero(String texto)
        {
            return texto.ToCharArray().Where(x => !Char.IsDigit(x)).Count() == 0;
        }

        #endregion
    }
}
